//
//  GXFeatureCommonDetailController.m
//  YIChuSeller
//
//  Created by 田小椿 on 2018/12/21.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXFeatureCommonDetailController.h"
#import "GXAddContentModel.h"
#import "GXAddContentCell.h"
@interface GXFeatureCommonDetailController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView  * detailTableView;
@property (nonatomic,strong)NSMutableArray * dataArray;
@end
static NSString *const addContentCell=@"GXAddContentCell";
@implementation GXFeatureCommonDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增";
    self.view.backgroundColor = kGlobalbgColor;
    [self.view addSubview:self.detailTableView];
    [self requestData];
}
- (void)requestData
{
    self.dataArray = [NSMutableArray array];
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [MenuContentManager selectMenuListWithStoreID:storeId success:^(id responseObject) {
        if (responseObject) {
            [self handleMessageData:responseObject];
        }
    } fail:^(id responseObject) {
        
    }];
}
- (void)handleMessageData:(id)responseObj
{
    NSDictionary *contentDic = responseObj[@"result"];
    if (![contentDic[@"services"] count]) {
        [SVProgressHUD showImage:nil status:@"无数据"];
    }else
    {
        for (NSDictionary * dict in contentDic[@"services"]) {
            GXAddContentModel * model = [GXAddContentModel mj_objectWithKeyValues:dict];
            [self.dataArray addObject:model];
        }
    }
    [self.detailTableView reloadData];
}


#pragma mark -- 创建uitableview
- (UITableView *)detailTableView
{
    if (_detailTableView == nil) {
        _detailTableView =[[UITableView alloc] initWithFrame:CGRectMake(10,15,kUIScreenWidth-20,kUIScreenHeight-Height_TabBar-Height_StatusBar-30) style:UITableViewStyleGrouped];
        _detailTableView.delegate=self;
        _detailTableView.dataSource=self;
        _detailTableView.sectionHeaderHeight = 5;
        _detailTableView.sectionFooterHeight = 0.01;
        _detailTableView.showsHorizontalScrollIndicator=NO;
        _detailTableView.showsVerticalScrollIndicator=NO;
        _detailTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _detailTableView.backgroundColor = [UIColor whiteColor];
        _detailTableView.layer.masksToBounds = YES;
        _detailTableView.layer.cornerRadius = 5;
        [_detailTableView registerNib:[UINib nibWithNibName:@"GXAddContentCell" bundle:nil] forCellReuseIdentifier:addContentCell];
    }
    return _detailTableView;
}

#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GXAddContentCell * cell = [tableView dequeueReusableCellWithIdentifier:addContentCell];
    GXAddContentModel * model = self.dataArray[indexPath.section];
    cell.model = model;
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GXAddContentModel * model = self.dataArray[indexPath.section];
    [self requestAddFeatureFuctionData:model.serviceId];
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
- (void)requestAddFeatureFuctionData:(NSString *)serviceId
{
    [MenuContentManager addFeatureFuctionWithServiceId:serviceId sige:self.signStr success:^(id responseObject) {
        if (responseObject) {
            if (self.addBlock){
                self.addBlock(self.signStr);
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
    } fail:^(id responseObject) {
    }];
}



@end
